package cloudspace.ui.applet.lang;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cloudspace.ui.applet.AppletVM;
import cloudspace.ui.applet.CloudResult;
import cloudspace.ui.applet.CloudToApplet;
import cloudspace.ui.applet.JSFactory;
import cloudspace.ui.applet.io.CommandHandler;


public class CloudSystem
{
    public static String getProperty( String key )
    {
        CloudResult result = AppletVM.callJScriptFunc( GET_JS_FUNC, key );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw new RuntimeException( throwing );
        }
        return result.getResult( 0 );

    }


    public static String getProperty( String key, String def )
    {
        CloudResult result = AppletVM.callJScriptFunc( GET_JS_FUNC, key );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw new RuntimeException( throwing );
        }
        if ( result.getResult( 0 ) == null )
            return def;
        else
            return result.getResult( 0 );
    }


    public static String setProperty( String key, String value )
    {
        CloudResult result = AppletVM.callJScriptFunc( SET_JS_FUNC, key, value );
        if ( result.isError() )
        {
            Throwable throwing = result.getException();
            throw new RuntimeException( throwing );
        }
        return result.getResult( 0 );
    }

    private static final String GET_JS_FUNC = "systemGetProperty";

    private static final String GET_EVENT = "onSystemGetProperty";

    private static final String[] GET_PARAM = new String[] { "key" };

    private static String GET_FUNC = JSFactory.generateJSFunction( GET_JS_FUNC,
        GET_EVENT,
        GET_PARAM );

    private static final String SET_JS_FUNC = "systemSetProperty";

    private static final String SET_EVENT = "onSystemSetProperty";

    private static final String[] SET_PARAM = new String[] { "key", "value" };

    private static String SET_FUNC = JSFactory.generateJSFunction( SET_JS_FUNC,
        SET_EVENT,
        SET_PARAM );


    private static class GetHandler implements CommandHandler
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> objects ) throws Exception
        {
            String key = (String)params.get( "key" );
            // System.out.println("Getting "+ key);
            return new CloudToApplet( System.getProperty( key ) );

        }

    }


    private static class SetHandler implements CommandHandler
    {

        public CloudToApplet handleRequest(
            Map<String, Object> params,
            Map<String, Object> objects ) throws Exception
        {
            String key = (String)params.get( "key" );
            String value = (String)params.get( "value" );
            // System.out.println("Setting "+key+" to "+ value);
            return new CloudToApplet( System.setProperty( key, value ) );

        }

    }

    private static final List<String> localScripts = new ArrayList<String>();

    private static final Map<String, CommandHandler> localEvents = new HashMap<String, CommandHandler>();
    static
    {
        localScripts.add( GET_FUNC );
        localScripts.add( SET_FUNC );
        localEvents.put( GET_EVENT, new GetHandler() );
        localEvents.put( SET_EVENT, new SetHandler() );

    }


    public static List<String> getJavaScript()
    {
        return localScripts;
    }


    public static Map<String, CommandHandler> getCommandHandlers()
    {
        return localEvents;
    }
}
